package vn.com.vega.chacha.v2.fragment;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.SearchScreen;
import vn.com.vega.music.database.DataStore;
import vn.com.vega.music.objects.SearchItem;
import vn.com.vega.music.utils.AnimationChaCha;
import vn.com.vega.music.view.adapter.SelectHistoryAdapter;
import vn.com.vega.music.view.adapter.SelectHistoryAdapter.SelectHistoryListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class ShowSearchHistoryFragment extends Fragment implements
		OnClickListener {
	private ImageView mCheckAll;
	private ListView mListHistory;
	private SelectHistoryAdapter mAdapter;
	private Button mDeleteHistory;
	private SearchScreen mSearchScreen;
	private ArrayList<SearchItem> listItemSearch = new ArrayList<SearchItem>();
	private DataStore mDataStore;
	private Toast mToast;

//	String[] fortest = { "Search 1", "Search 2", "Search 3", "Search 4",
//			"Search 1", "Search 2", "Search 3", "Search 4", "Search 1",
//			"Search 2", "Search 3", "Search 4", "Search 1", "Search 2",
//			"Search 3", "Search 4", "Search end" };

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bt_delete_search_history:
			List<SearchItem> listSelect = mAdapter.getListSelected();
			DeleteHistory deleteHistory = new DeleteHistory(this, listSelect);
			deleteHistory.start();
			for (int i = 0; i < listSelect.size(); i++) {
				SearchItem item = listSelect.get(i);
				listItemSearch.remove(item);
			}
			mAdapter.notifyDataSetChanged();
			mDeleteHistory.setVisibility(View.GONE);
			mCheckAll.setSelected(false);
			break;
		case R.id.cb_select_all_song:
			if (mCheckAll.isSelected()) {
				mCheckAll.setSelected(false);
				mAdapter.setSelectNone();
			} else {
				mCheckAll.setSelected(true);
				mAdapter.setSelectAll();
			}
			break;

		default:
			break;
		}
	}
	
	static class DeleteHistory extends Thread {
		WeakReference<ShowSearchHistoryFragment> showSearchHistoryFragmentRfc;
		List<SearchItem> listSearchItem;
		
		public DeleteHistory(ShowSearchHistoryFragment showSearchHistoryFragment, List<SearchItem> listSearchItem) {
			showSearchHistoryFragmentRfc = new WeakReference<ShowSearchHistoryFragment>(showSearchHistoryFragment);
			this.listSearchItem = listSearchItem;
		}
		
		@Override
		public void run() {
			try {
				ShowSearchHistoryFragment showSearchHistoryFragment = showSearchHistoryFragmentRfc.get();
				List<String> listSearchKey = new ArrayList<String>();
				for (SearchItem item : listSearchItem) {
					listSearchKey.add(item.searchKey);
				}
				showSearchHistoryFragment.mDataStore.deleteSearchItem(listSearchKey);
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}
	
	private void showToast(String message) {
		if (mToast == null) {
			mToast = Toast.makeText(mSearchScreen, "", Toast.LENGTH_LONG);
		}
		mToast.setText(message);
		mToast.show();
	}
	
//	private static final int DELETE_SEARCH_ITEM_SUCCESS = 0;
//	private static final int DELETE_SEARCH_ITEM_FAIL = 1;
//	
//	private ShowSearchHistoryhandler handler = new ShowSearchHistoryhandler(this);
//	
//	static class ShowSearchHistoryhandler extends Handler {
//		WeakReference<ShowSearchHistoryFragment> showSearchHistoryRfc;
//		
//		public ShowSearchHistoryhandler(ShowSearchHistoryFragment showSearchHistoryFragment) {
//			showSearchHistoryRfc = new WeakReference<ShowSearchHistoryFragment>(showSearchHistoryFragment);
//		}
//		
//		
//		@Override
//		public void handleMessage(Message msg) {
//			// TODO Auto-generated method stub
//			ShowSearchHistoryFragment showSearchHistoryFragment = showSearchHistoryRfc.get();
//			if (showSearchHistoryFragment == null || showSearchHistoryFragment.mSearchScreen.isDestroy) {
//				return;
//			}
//			switch (msg.what) {
//			case DELETE_SEARCH_ITEM_FAIL:
//				showSearchHistoryFragment.mAdapter.notifyDataSetChanged();
//				showSearchHistoryFragment.showToast(showSearchHistoryFragment.mSearchScreen.getString(R.string.delete_search_history_fail));
//				break;
//			case DELETE_SEARCH_ITEM_SUCCESS:
//				showSearchHistoryFragment.mAdapter.notifyDataSetChanged();
//				showSearchHistoryFragment.showToast(showSearchHistoryFragment.mSearchScreen.getString(R.string.delete_search_history_success));
//				break;
//			default:
//				break;
//			}
//			
//		}
//		
//	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mSearchScreen = (SearchScreen) getActivity();
		init();
	}

	private void init() {
//		for (int i = 0; i < fortest.length; i++) {
//			SearchItem item = new SearchItem(fortest[i], i, 0);
//			listItemSearch.add(item);
//		}
		mDataStore = DataStore.getInstance();
		listItemSearch = mDataStore.getAllSearchItem();
		mAdapter = new SelectHistoryAdapter(mSearchScreen, listItemSearch,
				mListener);
		mListHistory.setAdapter(mAdapter);
		mListHistory.setOnItemClickListener(songItemClickListener);
	}

	private SelectHistoryListener mListener = new SelectHistoryListener() {

		@Override
		public void changeNumberSongSelect() {
			// TODO Auto-generated method stub
			if (mAdapter == null) {
				return;
			}
			if (mAdapter.numberSelect <= 0) {
				if (mDeleteHistory.isShown()) {
					mDeleteHistory.startAnimation(AnimationChaCha
							.outToBottom(200));
					mDeleteHistory.setVisibility(View.GONE);
				}
			} else {
				if (mAdapter.numberSelect == mAdapter.getCount()) {
					mCheckAll.setSelected(true);
					mDeleteHistory.setText(mSearchScreen
							.getString(R.string.delete_all_search_history));
				} else {
					mCheckAll.setSelected(false);
					mDeleteHistory.setText(mSearchScreen
							.getString(R.string.delete_search_history_select));
				}
				if (!mDeleteHistory.isShown()) {
					mDeleteHistory.startAnimation(AnimationChaCha
							.infFromBottom(200));
					mDeleteHistory.setVisibility(View.VISIBLE);
				}
			}
		}
	};

	AdapterView.OnItemClickListener songItemClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			mSearchScreen.doSearch(((SearchItem)mAdapter.getItem(arg2)).searchKey);
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View root = inflater.inflate(R.layout.view_search_history, null);
		mCheckAll = (ImageView) root.findViewById(R.id.cb_select_all_song);
		mListHistory = (ListView) root.findViewById(R.id.lv_list_song_select);
		mDeleteHistory = (Button) root
				.findViewById(R.id.bt_delete_search_history);
		mCheckAll.setOnClickListener(this);
		mDeleteHistory.setOnClickListener(this);
		return root;
	}

}
